﻿using System;
using FluentValidation;
using FubuMVC.Core.Continuations;
using FubuMVC.Validation;
using StructureMap;
using StructureMap.Configuration.DSL;

namespace complex.Registries
{
    public class ValidationRegistry
        : Registry
    {
        public ValidationRegistry()
        {
            Scan(x =>
                     {
                         x.TheCallingAssembly();
                         x.ConnectImplementationsToTypesClosing(typeof (IValidator<>));
                     });
            For<IValidatorFactory>().Use<StructureMapValidatorFactory>();
            For<IContinuationDirector>().Use<ValidationContinuationHandler>();
        }
    }

    public class StructureMapValidatorFactory
        : ValidatorFactoryBase
    {
        private readonly IContainer container;

        public StructureMapValidatorFactory(IContainer container)
        {
            this.container = container;
        }

        public override IValidator CreateInstance(Type validatorType)
        {
            return (IValidator) container.GetInstance(validatorType);
        }
    }
}